package org.example.network1.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server01 {

    public static void main(String[] args) throws IOException {

        // 创建一个服务器
        ServerSocket serverSocket = new ServerSocket(8888);

        while(true)
        {
            // 启动服务器 接收客户端的连接
            Socket socket = serverSocket.accept();

            System.out.println("有客户端连接到服务器了");

            PrintStream printStream = new PrintStream(socket.getOutputStream());

            // 接收任意时刻客户端发送过来的数据
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String line = null;

            while( (line = bufferedReader.readLine()) != null )
            {
                System.out.println("接收到客户端发送过来的数据： " + line);

                printStream.println("return: " + line);
                printStream.flush();
            }

        }

    }

}
